﻿using UnityEngine;
using System.Collections;

public class Joy : MonoBehaviour {

	public GameObject player;
	private CharacterController cc;
	public Animator anim;

	public CC  camScale;

	public  float joyPositionX;
	public  float joyPositionY;




	void Awake(){
		cc = this.GetComponent < CharacterController>();
		anim=this.GetComponent<Animator>();
	}
	void OnEnable()  
	{  
		EasyJoystick.On_JoystickMove += OnJoystickMove;  
		EasyJoystick.On_JoystickMoveEnd += OnJoystickMoveEnd;  

	}  
	
	
	//移动摇杆结束  
	void OnJoystickMoveEnd(MovingJoystick move)  
	{  
		//停止时，角色恢复idle  
		if (move.joystickName == "MoveJoystick")  
		{  
			camScale.enabled=true;

			anim.SetBool("Walk",false);

		}
	}
	
	//移动摇杆中  
	void OnJoystickMove(MovingJoystick move)  
	{  
		//left

		if (move.joystickName != "MoveJoystick")  
		{  
			return;  
		}  
		
		//获取摇杆中心偏移的坐标  
		joyPositionX = move.joystickAxis.x;  
		joyPositionY = move.joystickAxis.y;  

		
		if (joyPositionY != 0 || joyPositionX != 0)  
		{  
			//设置角色的朝向（朝向当前坐标+摇杆偏移量）  
			transform.LookAt(new Vector3(transform.position.x + joyPositionX, transform.position.y, transform.position.z + joyPositionY));  
			//移动玩家的位置（按朝向位置移动）  
			cc.SimpleMove(transform.forward * 6);
			//transform.Translate(Vector3.forward * Time.deltaTime * 6);  
			//播放奔跑动画  
			camScale.enabled=false;

			anim.SetBool("Walk",true);
		}  
	}

	private void LateUpdate()
	{
		Camera.main.camera.transform.position = new Vector3 (player.transform.position.x+10f,player.transform.position.y+20f,player.transform.position.z-15f);
	}
}